using System;
using System.Collections;
using System.Collections.Generic;
using BitMango.Diagnostics;
using BMCommon;
using UnityEngine;

public class BannerAnchor : MonoBehaviour
{
    public static bool BannerActive
    {
        get
        {
            return BannerAnchor.bannerActive;
        }
        set
        {
            if (value != BannerAnchor.bannerActive)
            {
                BannerAnchor.bannerActive = value;
                foreach (BannerAnchor bannerAnchor in BannerAnchor.bannerAnchors)
                {
                    bannerAnchor.UpdatePositionInternal(OrientationObserver.Orientation);
                }
            }
        }
    }

    private void OnEnable()
    {
        OrientationObserver.onChange = (Action<ScreenOrientation>)Delegate.Combine(OrientationObserver.onChange, new Action<ScreenOrientation>(this.UpdatePosition));
        this.UpdatePosition();
        BannerAnchor.bannerAnchors.Add(this);
    }

    private void OnDisable()
    {
        OrientationObserver.onChange = (Action<ScreenOrientation>)Delegate.Remove(OrientationObserver.onChange, new Action<ScreenOrientation>(this.UpdatePosition));
        BannerAnchor.bannerAnchors.Remove(this);
    }

    public void UpdatePosition()
    {
        base.StartCoroutine(this.coUpdatePosition(OrientationObserver.Orientation));
    }

    private void UpdatePosition(ScreenOrientation _orient)
    {
        base.StartCoroutine(this.coUpdatePosition(_orient));
    }

    private IEnumerator coUpdatePosition(ScreenOrientation _orient)
    {
        yield return null;
        this.UpdatePositionInternal(_orient);
        yield break;
    }

    private void UpdatePositionInternal(ScreenOrientation _orient)
    {
        Camera camera = Scene.FindCameraForObject(base.gameObject);
        Debugger.Assert(camera != null, "BannerAnchor.UpdatePosition() Camera is null : Go[" + base.gameObject.name + "]");
        float num = BMTool.ConvertMillimeterToWorldLength(camera, this.offsetMilli);
        float num2 = 0f;
        if (BannerAnchor.bannerActive)
        {
        }
        tk2dCamera tk2dCamera = camera.GetComponent<tk2dCamera>();
        if (tk2dCamera.InheritConfig != null)
        {
            tk2dCamera = tk2dCamera.InheritConfig;
        }
        Rect screenExtents = tk2dCamera.ScreenExtents;
        Vector3 position = camera.transform.position;
        if (tk2dCamera.CameraSettings.orthographicOrigin == tk2dCameraSettings.OrthographicOrigin.BottomLeft)
        {
            position.y = position.y + screenExtents.y + num2 + num;
        }
        else
        {
            position.y = position.y - screenExtents.height * 0.5f + num2 + num;
        }
        Vector3 position2 = base.transform.position;
        position2.y = position.y;
        base.transform.position = position2;
    }

    public float offsetMilli;

    private static bool bannerActive = true;

    private static List<BannerAnchor> bannerAnchors = new List<BannerAnchor>();
}
